package bookstudytest.io;


import java.io.*;
import java.util.Scanner;

/**
 * @Auther: Lee
 * @Date: 2018/6/22 10:03
 * @Description:
 */
public class Test {
    public static void main(String[] args) throws IOException {
//        //在try后添加小括号建立资源，则不用在finally中显式关闭资源，会自动关闭资源 java 1.7版本后S
//        try (OutputStreamWriter aa = new OutputStreamWriter(new DataOutputStream(new FileOutputStream(new File("aa"))))) {
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        readTxtFile("C:/Users/Administrator/Desktop/1212.txt");
//        writeTxt("C:/Users/Administrator/Desktop/1212.txt", "你好啊 大哥！");
//        operationFile();
        printWriterFile();
    }

    /**
     * 读txt文件内容
     *
     * @param filePath 文件路径
     * @throws IOException
     */
    public static void readTxtFile(String filePath) throws IOException {

        File file = new File(filePath);
        Long length = file.length();
        int i = length.intValue();
        byte[] temp = new byte[i];
        //文件不存在会抛出异常
        FileInputStream input = new FileInputStream(file);
        int read = input.read(temp);
        //在流中读取的字节数组转为字符串时，需要用在文本中的编码转为字符串
        //这里先使用GBK编码生成字符串，然后字符串以utf-8编码生成字节数组，在用utf-8编码将生成的字节数组转为字符串
        byte[] utf8 = new String(temp, "GBK").getBytes("utf-8");
        System.out.println(new String(utf8, "utf-8"));
        input.close();
    }

    public static void printWriterFile() throws FileNotFoundException {
//        PrintWriter pw = new PrintWriter("D:/operationfileDemo/demo/textDemo.txt");
//        pw.println("ccccccccccccccccccccccccccccccccccc");
//        pw.flush();
//        pw.println("ddddddddddddddddddddddddddddddddddd");
//        pw.flush();
//        pw.close();
        //扫描文件
        File file = new File("D:/operationfileDemo/demo/textDemo.txt");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) {
            String next = scanner.next();
            System.out.println(next);
        }
    }

    /**
     * 向txt文件中写数据
     *
     * @param filePath
     * @param operationStr
     */
    public static void writeTxt(String filePath, String operationStr) throws IOException {
        File file = new File(filePath);

        //第二个参数 如果需要覆盖文件中的原数据 为false， 如果在原数据后面直接添加 则为true。
        FileOutputStream out = new FileOutputStream(file, true);
        BufferedOutputStream buffout = new BufferedOutputStream(out);
        //line.separator换行符
        String s = System.getProperty("line.separator") + "这里开始是重新添加的:" + operationStr + System.getProperty("line.separator");
        buffout.write(s.getBytes("GBK"));
        buffout.close();
    }

    /**
     * 文件的操作 新建 复制等
     */
    public static void operationFile() throws IOException {
        /**
         * 新建文件夹
         */
        File file = new File("D:/operationfileDemo/demo");
        //必须一级一级创建，比如下方的operationfileDemo文件夹如果不存在，那么demo1和assd、demo1都不可能创建出来
//        File file = new File("D:/operationfileDemo/demo1/assd/demo1");
        if (!file.exists()) {
            file.mkdir();
        }

        /**
         * 新建txt文件
         */
        File txtFile = new File("D:/operationfileDemo/demo/textDemo.txt");
        if (!txtFile.exists()) {
            txtFile.createNewFile();
            FileWriter fw = new FileWriter(txtFile);
            fw.write("我是用FileWriter来写的这段话，牛批不？");
            //类似于提交
            fw.flush();
            fw.close();
        }
        /**
         * 删除文件或文件夹
         */
        //删除文件    如果不存在不报错
//        File file1 = new File("D:/ddd/dd");
//        boolean delete = file1.delete();
//        File file2 = new File("D:/ddd/ea.txt");
//        boolean delete1 = file2.delete();
//        System.out.println(delete + "  " + delete1);
        //删除文件
//        if (txtFile.exists()) {
//            txtFile.delete();
//        }
//        //删除文件夹
        if (file.exists()) {
            //删除的文件夹为空才能删除
            boolean delete = file.delete();
            System.out.println(delete);
        }


    }


}
